import { defineStore } from "pinia";
import { ref, computed } from "vue"
import { Device } from '@/interface/home'
import { Storage } from "@/utils/storage";

export default defineStore('deviceStore', () => {
  // 已选中的设备列表 {nodeId: data}
  const checkedDeviceList = ref<{ [nodeId: string]: Device }>({})
  // 是否需要渲染摄像头
  const video = ref(false)
  // // 当前选中的机构id
  // const curSelectOrgId = ref<number>(Storage.getItem('selOrgId') || '')
  // 当前设备列表行选中的设备nodeId
  const checkedDeviceNodeId = ref<string>("")
  // 是否显示video组件
  const videoPageDisable = ref<boolean>(true)

  // 已选中设备的nodeId列表
  const checkedDeviceNodeIdList = computed(()=>{
    return Object.keys(checkedDeviceList.value)
  })
  // 已选中设备的信息列表
  const checkedDeviceInfoList = computed(()=>{
    return Object.values(checkedDeviceList.value) || []
  })
  // 当前选中设备的数据
  const checkedDeviceInfo = computed(()=>{

    return checkedDeviceList.value[checkedDeviceNodeId.value]
  })

  function clearState(){
    checkedDeviceList.value = {}
    checkedDeviceNodeId.value = ""
  }

  return {
    checkedDeviceList,
    checkedDeviceNodeIdList,
    checkedDeviceInfoList,
    video,
    videoPageDisable,
    checkedDeviceInfo,
    checkedDeviceNodeId,
    clearState
  }
})
